package cn.achao.csr.chat.core.event.listener;

import cn.achao.csr.api.model.dto.MsgSendMessageDTO;
import cn.achao.csr.base.constant.RockMqKeyConstants;
import cn.achao.csr.chat.core.event.MessageSendEvent;
import cn.achao.csr.mq.producer.CsrMqProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 消息发送监听器
 *
 * @author achao
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MessageSendListener {

    private final CsrMqProducer mqProducer;

    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = MessageSendEvent.class, fallbackExecution = true)
    public void messageRoute(MessageSendEvent event) {
        log.info("消息发送监听器：{}", event);
        Long msgId = event.getMsgId();
        mqProducer.sendSecureMsg(RockMqKeyConstants.SEND_MSG_TOPIC, new MsgSendMessageDTO(msgId), msgId);
    }


}
